<!-- 用户注册申请、引导下载软件 -->

<template>
  <div class="home">
    <div class="applyAndDown_idex1">
      <div class="titleTop"></div>
      <!-- <div class="title">支付宝红包券 抢百种福利</div> -->
      <!-- <div class="title2">新用户审核通过率高达92.58%</div> -->
      <!-- <van-image width="100%" height="39" :src="imageUrl" /> -->
      <div class="tags">
        <div v-for="(text, index) in tagsArr" :key="index" class="tags-item">
          <van-image :src="tickImg" width="16" height="16"></van-image
          ><span class="tags-text">{{ text }}</span>
        </div>
      </div>
      <div class="applyAndDown_idex2">
        <div class="middle-top">仅凭身份证可领取消费额度</div>
        <div class="money">最高可用消费额度(元)</div>
        <div class="dinmoney">200,000</div>
        <div class="interest">享品牌数码折扣</div>
        <van-cell-group class="imgCodeGroup" style="margin-top: 20px" inset>
          <van-field
            class="imgCode"
            v-model="formData.telPhoneNum"
            :formatter="formatter"
            required
            placeholder="请输入手机号"
          >
            <template #left-icon>
              <div class="phone-icon-image">
                <van-image :src="phoneIcon" width="22" height="22"></van-image>
              </div>
            </template>
          </van-field>
        </van-cell-group>
        <van-cell-group class="imgCodeGroup" inset>
          <van-field
            class="imgCode"
            v-model="formData.telPhoneMessage"
            center
            clearable
            placeholder="请输入验证码"
            :radius="8"
          >
            <template #left-icon>
              <div class="phone-icon-image">
                <van-image :src="codeIcon" width="22" height="22"></van-image>
              </div>
            </template>
            <template #button>
              <div class="code-img" @click="refreshCodeImg">
                <van-image :src="codeImg" height="32" width="86"></van-image>
              </div>

              <!-- <van-button
                v-if="sendCodeClicked"
                size="small"
                type="primary"
                color="rgb(254 173 199)"
                @click="sendVerificationCode"
                >获取验证码</van-button
              >
              <span v-if="!sendCodeClicked">{{ countdown }}秒后重新获取</span> -->
            </template>
          </van-field>
        </van-cell-group>
        <div style="margin: 16px">
          <van-button
            class="submitBtn"
            block
            round
            native-type="submit"
            @click="onsubmit"
            color="#FD2F70"
            >下一步</van-button
          >
        </div>
        <div class="loginAgreement">
          <van-checkbox
            v-model="formData.boxChecked"
            checked-color="#FD2F70"
            icon-size="14px"
          ></van-checkbox>
          <span style="margin-left: 8px">我已阅读并同意</span
          ><a href="/userAgreement">《软件注册协议》</a
          ><a href="/privacyAgreement">《隐私协议》</a>
        </div>
      </div>
    </div>
  </div>
</template>

<script lang="ts">
import { sendCode, submit, countUV, getCodeImg } from "@/api/user";
import { showToast } from "vant";
import myImage from "@/assets/line.png";
import { useUserStroe } from "@/pinia/modules/main";
import { useRouter } from "vue-router";
import tickImg from "@/assets/channel/tick.png";
import phoneIcon from "@/assets/channel/phone-icon.png";
import codeIcon from "@/assets/channel/code-icon.png";

const userStore = useUserStroe();

// const router = useRouter();

export default {
  name: "applyAndDown",
  components: {},
  data() {
    return {
      key: "",
      codeImg: "",
      codeIcon,
      phoneIcon,
      tagsArr: ["审核快", "额度高", "周期长", "品类多"],
      imageUrl: myImage,
      tickImg: tickImg,
      formData: {
        //test
        // telPhoneNum: "19147196688",
        //
        telPhoneNum: "",
        telPhoneMessage: "",
        boxChecked: false,
        // telPhoneNum: "13122780440",
        // telPhoneMessage: "000000",
        // boxChecked: true,
      },
      sendCodeClicked: true,
      timer: undefined,
      countdown: 60,
    };
  },

  async mounted() {
    console.log("query code: ", this.$route.query.code);
    let channelCode: string = this.$route.query.code as string;
    const [res, _] = await [countUV(channelCode), this.refreshCodeImg()];
    if (res.code != 200) {
      console.log(res.msg);
    }
  },

  watch: {
    countdown(val) {
      if (val <= 0) {
        this.sendCodeClicked = true;
        clearInterval(this.timer);
        this.countdown = 60;
      }
    },
  },
  methods: {
    async refreshCodeImg() {
      const res = await getCodeImg();
      if (res.code == 200) {
        this.codeImg = res.data.image;
        this.key = res.data.key;
      } else {
        showToast("网络开了会小差，请稍后重试！");
      }
    },
    formatter(value) {
      // 只保留数字，并限制长度为 11
      return value.replace(/\D/g, "").slice(0, 11);
    },
    async sendVerificationCode() {
      const { telPhoneNum } = this.formData;
      if (telPhoneNum) {
        try {
          await sendCode({ phone: telPhoneNum });
          console.log("request1");
          showToast("验证码已发送，10分钟内有效");
          // 设置定时器
          this.timer = setInterval(() => {
            this.countdown--;
          }, 1000);
          this.sendCodeClicked = false;
        } catch (error) {
          console.log(error, "request2");
          this.countdown = 0;
          this.sendCodeClicked = true;
        }
      } else {
        showToast("请先填写手机号");
      }
    },
    async onsubmit() {
      const { telPhoneNum, telPhoneMessage, boxChecked } = this.formData;
      if (telPhoneNum) {
        if (telPhoneMessage) {
          if (boxChecked) {
            const res = await submit(
              {
                channelCode: this.$route.query.code,
                phone: telPhoneNum,
                code: telPhoneMessage,
                grantType: "phone",
                // grantType: "captcha",
                external: "1", //图片验证码专用
              },
              this.key
            );
            // console.log("res: ", res);
            if (res.code == 200) {
              showToast("注册成功");
              // 存token和用户相关信息
              userStore.setToken(res.data?.accessToken);
              userStore.setUserInfo({ phone: this.formData.telPhoneNum });
              this.$router.push("/AuthPage");
              // this.$router.push("/guide");
            } else {
              showToast(res.msg);
            }
          } else {
            showToast("请先同意协议");
          }
        } else {
          showToast("请先填写验证码");
        }
      } else {
        showToast("请先填写手机号");
      }
    },
  },

  beforeDestroy() {
    console.log("-----+++++++");
    clearInterval(this.timer);
  },
};
</script>

<style lang="scss">
.middle-top {
  position: absolute;
  top: 1px;
  right: 0;
  font-size: 12px;
  padding: 10px 12px;
  color: #fff;
  border-bottom-left-radius: 18px;
  border-top-right-radius: 18px;
  background-color: #fd2f70;
  // background-size: contain;
  // background-repeat: no-repeat;
  // height: 40px;
  // width: 210px;
  // line-height: 44px;
  text-align: center;
  // background-image: url("@/assets/channel/home-middle-top-bg.png");
}
.applyAndDown_idex1 {
  min-height: 100%;
  background-color: rgb(246 246 246);
  // padding: 14px;
  padding-top: 20px;
  width: 100%;
  // height: 330px;
  // background-size: cover;
  background-size: contain;
  background-repeat: no-repeat;
  background-image: url("../assets/channel/home-bg.png");
  .titleTop {
    height: 200px;
    margin-left: 15px;
    // height: 24px;
    font-family: FZH;
    font-weight: 400;
    font-size: 20px;
    color: #fff;
    line-height: 33px;
  }
  .title {
    margin-top: 37px;
    text-align: center;
    width: 100%;
    height: 34px;
    // font-family: FZH;
    font-weight: 400;
    font-size: 26px;
    color: #fff;
    line-height: 34px;
  }
  .title2 {
    width: 100%;
    height: 17px;
    font-weight: 500;
    font-size: 12px;
    text-align: center;
    color: #fff;
    line-height: 14px;
    margin-bottom: 18px;
  }
  .applyAndDown_idex2 {
    position: relative;
    margin: 0 auto;

    margin-top: 16px;
    padding: 22px 4px 0;
    width: 92%;
    height: 455px;
    background-size: 100% 100%;
    background-repeat: no-repeat;
    background-image: url("../assets/channel/home-middle-bg.png");
    // background-image: url("../assets/bg2.png");
    .money {
      margin: 34px auto 16px;
      width: 100%;
      height: 23px;
      font-family: PingFang SC;
      text-align: center;
      font-weight: 800;
      font-size: 17px;
      color: #333;
    }
    .dinmoney {
      width: 100%;
      height: 59px;
      font-family: DIN;
      font-weight: 700;
      font-size: 45px;
      color: #333;
      line-height: 49px;
      text-align: center;
    }
    .interest {
      text-align: center;
      width: 100%;
      height: 14px;
      font-family: PingFang SC;
      font-weight: 500;
      font-size: 12px;
      color: #333;
      margin-bottom: 25px;
    }
    .imgCodeGroup {
      --van-cell-group-inset-radius: 14px;
      margin: 14px 15px 0;
      text-align: center;
      background: #fff;
    }
    .submitBtn {
      // background-color: #ffffff;
      border-radius: 14px;
      font-size: 17px;
      color: #fff;
      font-weight: bold;
    }
    .loginAgreement {
      display: flex;
      justify-content: center;
      font-size: 12px;
      align-items: center;
      color: rgb(245, 219, 219);
      span {
        color: #727272;
      }
      a {
        color: #fc578b;
      }
    }
  }
}
.tags {
  padding: 14px 4px;
  // background-color: #fe4062;
  font-size: 12px;
  background: linear-gradient(to bottom, #fe4062, #fd6e8a);
  display: flex;
  justify-content: space-around;
  &-item {
    display: flex;
    align-items: center;
  }
  .tags-text {
    color: #fff;
    margin-left: 6px;
  }
}
.phone-icon-image {
  height: 100%;
  display: flex;
  align-items: center;
}
.wrapper-field {
  border-radius: 10px;
  overflow: hidden;
}
.code-img {
  display: flex;
  align-items: center;
}
</style>
